package com.zzptc.zzsgz.bdsjws.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.services.IVersionUpdate;
import com.zzptc.zzsgz.bdsjws.services.MyDownLoadService;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;
import com.zzptc.zzsgz.bdsjws.vo.VersionInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class CheckUpdateBroadcast extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, final Intent intent) {


//        //访问网络   每隔7天   获取versioncode   使用retrofit2框架访问网络
//        //	首先要知道上一次检查更新的日期  小String  我们用共享首选项来保存
        SharedPreferences sharedPreferences= MyUtil.getSharedPre();//获取共享首选项
        String olddate=  sharedPreferences.getString("checkDate",null);
        Date newdate=new Date();//当前日期
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String newdateformat=sdf.format(newdate);
        //sdf.format(日期类)   由日期类转成String
        // sdf.parse("")  将String转成日期类
        try {
            Date checkdate = null;
                if (olddate != null){
                    checkdate = sdf.parse(olddate);
                }

                if (olddate == null || "".equals(olddate)) {
                    //表示是第一次安装   将当前的日期存储在共享首选项
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("checkDate", newdateformat);//存入之前要将Date转成String
                    editor.commit();

                } else {
                    //计算日期差
                    int days = MyUtil.daysBetween(checkdate, newdate);
                    //Toast.makeText(context, days+"", Toast.LENGTH_SHORT).show();
                    if (days >= 7) {
                        //访问服务器  看看有没有新的版本号
                        Retrofit retrofit = new Retrofit.Builder()
                                // .baseUrl("http://10.0.2.2:8080/")
//                            .baseUrl("http://192.168.1.137:8080/")
                                .baseUrl("http://119.29.227.211/")
                                .addConverterFactory(GsonConverterFactory.create())
                                .build();
                        IVersionUpdate versionUpdate = retrofit.create(IVersionUpdate.class);//创建接口对象
                        Call<VersionInfo> versionInfoCall = versionUpdate.getVersion();
                        //将当前的日期存储在共享首选项  作为上次更新的日期
                        versionInfoCall.enqueue(new Callback<VersionInfo>() {
                            @Override
                            public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                                VersionInfo versionInfo = response.body();
                                int newversioncode = versionInfo.getVersionCode();//从服务器端获取的版本号
                                int oldeversioncode = MyUtil.getVersionCode(MyApplication.getContext());//正在使用的APP的版本号
                                if (newversioncode > oldeversioncode) {
                                    //进行强制更新
                                    Intent intent1 = new Intent();
                                    intent1.setClass(MyApplication.getContext(), MyDownLoadService.class);
                                    intent1.putExtra("apkurl", versionInfo.getApkUrl());
                                    MyApplication.getContext().startService(intent1);
                                }
                            }

                            @Override
                            public void onFailure(Call<VersionInfo> call, Throwable t) {

                            }
                        });
                    } else {

                    }

                }


        } catch (ParseException e) {
            e.printStackTrace();
        }





//        System.out.println("=========");
//                SharedPreferences sharedPreferences = MyUtil.getSharedPre();
//        String olddate = sharedPreferences.getString("checkDate",null);
//        Date newdate = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        //sdf.format()  由日期类转成String
//        //sdf.parse("")  将String转成日期类
//
//        try {
//            Date checkdate = null;
//                if (olddate != null){
//                    checkdate = sdf.parse(olddate);
//                }
//            //计算日期差
//            if (olddate == null || "".equals(olddate)){
//                //表示第一次安装 将当前的日期存储在共享首选项
//
//            }else{
//                //计算日期差
//            int days = MyUtil.daysBetween(checkdate,newdate);
//                Toast.makeText(context, days+"11111111111111111", Toast.LENGTH_SHORT).show();
//            }
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }


    }
}
